package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.iknow.IKnow;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FriendAdapter;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.util.StringUtils;

public class RosterActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private AbsListView.OnScrollListener ListViewOnScrollListener;
  private int lastItem;
  private AdapterView.OnItemClickListener listItemClickListener;
  private FriendAdapter mAdapter;
  private Button mBackButton;
  private boolean mBinded;
  private int mFirstVisibleItem;
  private List<Friend> mFriendList;
  private MyListView mList;
  private View mListViewFooter;
  private RelativeLayout mLoadingBar;
  private ProgressDialog mProgressDialog;
  private final ServiceConnection mServConn;
  private FriendTask mTask;
  private TaskListener mTaskListener;
  private int mType;
  private IXmppFacade mXmppFacade;
  private boolean needShow;
  private int offset;
  private PopupWindow pw;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public RosterActivity()
  {
    this.mServConn = new XmppServiceConnection(this, null);
    this.lastItem = 0;
    this.offset = 0;
    this.pw = null;
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        Friend localFriend = RosterActivity.access$0(this.this$0).getItem(paramInt);
        if (localFriend == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, FriendActivity.class);
          localIntent.putExtra("friend", localFriend);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.ListViewOnScrollListener = new AbsListView.OnScrollListener(this)
    {
      public void onScroll(, int paramInt1, int paramInt2, int paramInt3)
      {
        if (paramInt1 != RosterActivity.access$1(this.this$0))
          if ((paramInt1 + paramInt2 >= paramInt3) && (RosterActivity.access$2(this.this$0)))
            RosterActivity.access$3(this.this$0);
        while (true)
        {
          return;
          RosterActivity.access$4(this.this$0, paramInt1);
        }
      }

      public void onScrollStateChanged(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 2:
        case 1:
        case 0:
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              RosterActivity.access$0(this.this$0).setScrolling(true);
            }
            RosterActivity.access$0(this.this$0).setScrolling(true);
          }
          RosterActivity.access$0(this.this$0).scrollIdle(paramAbsListView);
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FriendTask";
      }

      public void onCancelled()
      {
        if (RosterActivity.access$6(this.this$0) != null)
        {
          RosterActivity.access$6(this.this$0).clear();
          RosterActivity.access$9(this.this$0, null);
        }
        if (RosterActivity.access$0(this.this$0) != null)
        {
          RosterActivity.access$0(this.this$0).clearFriend();
          RosterActivity.access$10(this.this$0, null);
        }
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        Iterator localIterator;
        RosterActivity.access$5(this.this$0).setVisibility(8);
        if (RosterActivity.access$6(this.this$0).size() < 10)
        {
          RosterActivity.access$7(this.this$0, false);
          localIterator = RosterActivity.access$6(this.this$0).iterator();
        }
        while (true)
        {
          while (!(localIterator.hasNext()))
          {
            RosterActivity.access$0(this.this$0).sortFriendList();
            RosterActivity.access$0(this.this$0).notifyDataSetChanged();
            RosterActivity.access$6(this.this$0).clear();
            if ((RosterActivity.access$8(this.this$0) != null) && (RosterActivity.access$8(this.this$0).isShowing()))
              RosterActivity.access$8(this.this$0).dismiss();
            return;
            RosterActivity.access$7(this.this$0, true);
          }
          Friend localFriend = (Friend)localIterator.next();
          RosterActivity.access$0(this.this$0).addFriend(localFriend);
        }
      }

      public void onPreExecute()
      {
        RosterActivity.access$5(this.this$0).setVisibility(0);
      }
    };
  }

  private void setupListViewFooter()
  {
    this.mListViewFooter = getLayoutInflater().inflate(2130903121, null);
    this.mLoadingBar = ((RelativeLayout)this.mListViewFooter.findViewById(2131296408));
    this.mList.addFooterView(this.mListViewFooter);
  }

  private void startGetFriendInfo()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new FriendTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  public LinearLayout linearContainer()
  {
    ProgressBar localProgressBar = new ProgressBar(this);
    TextView localTextView = new TextView(this);
    LinearLayout localLinearLayout = new LinearLayout(this);
    localLinearLayout.setOrientation(0);
    localTextView.setText("加载中...");
    localTextView.setGravity(16);
    localProgressBar.setPadding(0, 0, 15, 0);
    localLinearLayout.addView(localProgressBar, new LinearLayout.LayoutParams(-2, -2));
    localLinearLayout.addView(localTextView, new LinearLayout.LayoutParams(-2, -2));
    localLinearLayout.setGravity(17);
    localLinearLayout.setBackgroundColor(2131165193);
    return localLinearLayout;
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    this.mType = getIntent().getIntExtra("item_type", 3);
    this.mAdapter = new FriendAdapter(this, getLayoutInflater());
    requestWindowFeature(1);
    setContentView(2130903123);
    this.mFriendList = new ArrayList();
    this.mList = ((MyListView)findViewById(2131296466));
    this.mList.setOnItemClickListener(this.listItemClickListener);
    this.mList.setOnScrollListener(this.ListViewOnScrollListener);
    setupListViewFooter();
    this.mList.setAdapter(this.mAdapter);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
    findViewById(2131296344).setVisibility(8);
  }

  protected void onDestroy()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    if (this.mFriendList != null)
    {
      this.mFriendList.clear();
      this.mFriendList = null;
    }
    if (this.mAdapter != null)
    {
      this.mAdapter.clearFriend();
      this.mAdapter = null;
    }
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mServConn);
      this.mBinded = false;
    }
    super.onPause();
  }

  protected void onResume()
  {
    if ((((IKnow)getApplication()).isAccountConfigured()) && (!(this.mBinded)))
      this.mBinded = bindService(SERVICE_INTENT, this.mServConn, 1);
    super.onResume();
  }

  private class FriendTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      Object localObject3;
      List localList = IKnow.mIKnowDataBase.getMyRosterID(RosterActivity.access$12(this.this$0), RosterActivity.access$13(this.this$0), 10);
      Iterator localIterator = localList.iterator();
      if (!(localIterator.hasNext()))
      {
        if (!(localList.isEmpty()))
        {
          RosterActivity localRosterActivity = this.this$0;
          RosterActivity.access$15(localRosterActivity, RosterActivity.access$13(localRosterActivity) + localList.size());
        }
        return TaskResult.OK;
      }
      String str = (String)localIterator.next();
      Object localObject1 = IKnow.mIKnowDataBase.getFriendInfo(str);
      if (localObject1 == null);
      try
      {
        if ((RosterActivity.access$14(this.this$0).createConnection() != null) && (RosterActivity.access$14(this.this$0).createConnection().getIKnowUser() != null))
        {
          Friend localFriend = RosterActivity.access$14(this.this$0).createConnection().getIKnowUser().getUserInfo(StringUtils.parseName(str));
          localObject3 = localFriend;
          if (localObject3 == null);
        }
      }
      catch (RemoteException localRemoteException1)
      {
        Object localObject2;
        try
        {
          localObject1 = new Friend(str, null, null);
          while (true)
          {
            while (true)
              RosterActivity.access$6(this.this$0).add(localObject1);
            IKnow.mIKnowDataBase.addFriend((Friend)localObject3);
            localObject1 = localObject3;
          }
          localRemoteException1 = localRemoteException1;
          localObject2 = localRemoteException1;
          ((RemoteException)localObject2).printStackTrace();
        }
        catch (RemoteException localRemoteException2)
        {
          while (true)
          {
            while (true)
            {
              localObject2 = localRemoteException2;
              localObject1 = localObject3;
            }
            localObject3 = localObject1;
          }
        }
      }
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    private boolean mGetOnce;

    public void onServiceConnected(, IBinder paramIBinder)
    {
      RosterActivity.access$11(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      if (this.mGetOnce);
      while (true)
      {
        return;
        RosterActivity.access$3(this.this$0);
        this.mGetOnce = true;
      }
    }

    public void onServiceDisconnected()
    {
      RosterActivity.access$11(this.this$0, null);
    }
  }
}